package com.flying.carclub.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;


import com.flying.carclub.bean.UserInfoBean;

import java.util.Set;

/**
 * SharePreference 工具类
 * 
 * @ClassName: SPUtils
 * 
 * 
 */

public class SPUtils {
	static String SP_NAME = "sp_config";

	public static void putInt(Context context, String key, int value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putInt(key, value).apply();
	}

	public static void putFloat(Context context, String key, Float value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putFloat(key, value).apply();
	}

	public static void putLong(Context context, String key, Long value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putLong(key, value).apply();
	}

	public static void putBoolean(Context context, String key, Boolean value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putBoolean(key, value).apply();
	}

	public static void putString(Context context, String key, String value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putString(key, value).apply();
	}

	public static void putStringSet(Context context, String key,
			Set<String> value) {
		SharedPreferences sp = getSp(context);
		sp.edit().putStringSet(key, value).apply();
	}

	public static String getString(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getString(key, "");
	}

	public static String getString(Context context, String key, String defValue) {
		SharedPreferences sp = getSp(context);
		return sp.getString(key, defValue);
	}

	public static int getInt(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getInt(key, 0);
	}

	public static int getInt(Context context, String key, Integer defValue) {
		SharedPreferences sp = getSp(context);
		return sp.getInt(key, defValue);
	}

	public static Float getFloat(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getFloat(key, 0);
	}

	public static Float getFloat(Context context, String key, Float defValue) {
		SharedPreferences sp = getSp(context);
		return sp.getFloat(key, defValue);
	}

	public static Boolean getBoolean(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getBoolean(key, false);
	}

	public static Boolean getBoolean(Context context, String key,
			Boolean defValue) {
		SharedPreferences sp = getSp(context);
		return sp.getBoolean(key, defValue);
	}

	public static Long getLong(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getLong(key, 0);
	}

	public static Long getLong(Context context, String key, Long defValue) {
		SharedPreferences sp = getSp(context);
		return sp.getLong(key, defValue);
	}

	public static Set<String> getStringSet(Context context, String key) {
		SharedPreferences sp = getSp(context);
		return sp.getStringSet(key, null);
	}
	public static void clearSP(Context context){
		SharedPreferences sp = getSp(context);
		Editor edit = sp.edit();
		edit.clear();
		edit.apply();
	}

	/**
	 * 获取SharePreferences对象
	 * @Title: getSp
	 * @param context 上下文
	 * @return SharedPreferences 返回类型
	 */
	private static SharedPreferences getSp(Context context) {
		return context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
	}


	public static UserInfoBean getUserInfo(Context context){
		UserInfoBean userInfoBean = null;
		String username = getString(context,"username");
		String realname = getString(context,"realname");
		String nickname = getString(context,"nickname");
		int sex = getInt(context,"sex");
		if (!TextUtils.isEmpty(username)){
			userInfoBean = new UserInfoBean();
			userInfoBean.username = username;
			userInfoBean.realName = realname;
			userInfoBean.nickname = nickname;
			userInfoBean.sex = sex;
		}
		return userInfoBean;
	}
	public static void setUserInfo(Context context, UserInfoBean userInfo){
		if (userInfo != null){
			putString(context, "username", userInfo.username);
			putString(context, "realname", userInfo.realName);
			putString(context, "nickname", userInfo.nickname);
			putInt(context, "sex", userInfo.sex);
		}
	}
	public static void clearUserInfo(Context context){
		clearSP(context);
	}
}
